package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneAdditionalOfferInstService;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferInstDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdSubOfferModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdSubOfferTypeVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdSubOfferVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.CuccProdSubOfferVO;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.util.LocalAdditionalOfferUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.web.service.accept.constant.TemplateConsts;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.iwhalecloud.bss.kite.web.service.accept.util.AdditionalOfferUtils;
import com.ztesoft.bss.base.staticdata.CoreDataOffer;
import com.ztesoft.bss.common.util.SpringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <Description>可选包组件抽象类<br>
 *
 * @author huang.caibiao<br>
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.service.accept.handler.module.abs <br>
 * @since R9.0<br>
 */
public abstract class LocalAbstractAdditionalOfferModuleHandler extends AbstractModuleHandler {

    /**
     * 获取可选包组件对象
     * @param offerInstList 可选包实例
     * @param offerId 套餐或者礼包规格标识
     * @param prodId  产品标识
     * @param mustOfferIds  必选区展示
     * @return
     */
    protected ProdSubOfferModuleVO getAdditionalOfferModuleVO(List<OfferInstDTO> offerInstList, String offerId, String prodId, String roleId, List<String> mustOfferIds) {
        List<ProdSubOfferTypeVO> resultOfferList = new ArrayList<>();
        //必选可选包 包括促销
        List<ProdSubOfferVO> mustProdSubOfferList = genProdSubOfferList(CoreDataOffer.OFF_C_0005_1200, resultOfferList);

        //默认可选包 包括促销
        List<ProdSubOfferVO> defaultProdSubOfferList = genProdSubOfferList(CoreDataOffer.OFF_C_0005_1300, resultOfferList);

        //可选可选包
        List<ProdSubOfferVO> optionalProdSubOfferList = genProdSubOfferList(TemplateConsts.ADDITIONAL_OFFER_OPTION, resultOfferList);


        if (!KiteListUtils.isEmpty(offerInstList)) {

            List<CuccProdSubOfferVO> prodSubOfferList = LocalAdditionalOfferUtils.getInst().getProdSubOfferList(offerInstList, prodId, roleId);

            //挂在销售品上面的必选可选包
            final List<Offer> offerMustAdditionalOffers = OfferCache.get(offerId)
                .listMustAdditionalOffers(prodId);
            //挂在产品上面的必选可选包
            final List<Offer> productMustAdditionalOffers = OfferCache.get(ProductCache.get(prodId).getBaseOfferId())
                .listMustAdditionalOffers();
            //挂在产品上的必选可选包，成员带可选包100099的类型
            productMustAdditionalOffers.addAll(OfferCache.get(offerId)
                .listMustAdditionalProd());

            List<Offer> mustAdditionalOfferList = new ArrayList<>();
            mustAdditionalOfferList.addAll(offerMustAdditionalOffers);
            mustAdditionalOfferList.addAll(productMustAdditionalOffers);
            if (KiteListUtils.isNotEmpty(mustOfferIds)) {
                mustOfferIds.forEach(oId -> {
                    Offer offer = new Offer();
                    offer.setOfferId(oId);
                    mustAdditionalOfferList.add(offer);
                });
            }

            //区分必选默选
            genProdSubOfferInstList(prodSubOfferList, mustAdditionalOfferList, mustProdSubOfferList, defaultProdSubOfferList);

            if (KiteListUtils.isNotEmpty(defaultProdSubOfferList)) {
                //区分默选 可选
                final List<Offer> productDefaultAdditionalOffers = OfferCache.get(offerId).listDefaultAdditionalOffers(prodId);
                final List<Offer> offerDefaultAdditionalOffers = OfferCache.get(ProductCache.get(prodId).getBaseOfferId()).listDefaultAdditionalOffers();
                List<Offer> defaultAdditionalOfferList = new ArrayList<>();
                defaultAdditionalOfferList.addAll(offerDefaultAdditionalOffers);
                defaultAdditionalOfferList.addAll(productDefaultAdditionalOffers);

                dealUpdate(defaultProdSubOfferList, optionalProdSubOfferList, defaultAdditionalOfferList);
            }


        }

        //如果是必选和默选，如果是空则不返回给页面
        removeAdditionalOffer(resultOfferList, Arrays.asList(CoreDataOffer.OFF_C_0005_1200, CoreDataOffer.OFF_C_0005_1300));

        ProdSubOfferModuleVO prodSubOfferModuleVO = new ProdSubOfferModuleVO();
        prodSubOfferModuleVO.setProdSubOfferTypeList(resultOfferList);
        return prodSubOfferModuleVO;
    }



    /**
     * 处理修改的情况
     * @param defaultProdSubOfferList
     * @param optionalProdSubOfferList
     * @param defaultOfferList
     */
    protected void dealUpdate(List<ProdSubOfferVO> defaultProdSubOfferList, List<ProdSubOfferVO> optionalProdSubOfferList, List<Offer> defaultOfferList) {
        //修改的时候处理
        //可选可选包 包括促销  修改的时候带出来
        if (!KiteListUtils.isEmpty(defaultProdSubOfferList)) {
            Iterator<ProdSubOfferVO> iterator = defaultProdSubOfferList.iterator();
            while (iterator.hasNext()) {
                ProdSubOfferVO offerInst = iterator.next();
                if (!AdditionalOfferUtils.getInst().isContainSubOffer(defaultOfferList, offerInst.getOfferId())) {
                    //如果不是默选的，那就从默选列表中删除，并放到可选列表中
                    iterator.remove();
                    optionalProdSubOfferList.add(offerInst);
                }
            }
        }
    }


    /**
     * 根据attrValue来移除空的对象，让页面不展示
     * @param result 可选包返回页面对象
     * @param attrValues 可选包限制值
     */
    protected void removeAdditionalOffer(List<ProdSubOfferTypeVO> result, List<String> attrValues) {
        Iterator<ProdSubOfferTypeVO> iterator = result.iterator();
        while (iterator.hasNext()) {
            ProdSubOfferTypeVO prodSubOfferTypeVO = iterator.next();
            if (attrValues.contains(prodSubOfferTypeVO.getRstrType())) {
                if (KiteListUtils.isEmpty(prodSubOfferTypeVO.getProdSubOfferList())) {
                    iterator.remove();
                }
            }
        }
    }


    /**
     * 构造空的可选包列表
     * @param attrValue
     * @return
     */
    protected List<ProdSubOfferVO> genProdSubOfferList(String attrValue, List<ProdSubOfferTypeVO> resultOfferList) {
        List<ProdSubOfferVO> prodSubOfferList = new ArrayList<>();
        ProdSubOfferTypeVO prodSubOfferType = new ProdSubOfferTypeVO();
        prodSubOfferType.setRstrTypeDesc(AttrValueCache
            .getAttrValueNameByAttrNbrAndValue(KeyConsts.OFFER_RSTR_TYPE, attrValue));
        prodSubOfferType.setRstrType(attrValue);
        prodSubOfferType.setProdSubOfferList(prodSubOfferList);
        resultOfferList.add(prodSubOfferType);
        return prodSubOfferList;
    }

    /**
     * 构造可选包实例列表
     *
     * @param additionalOfferInstList 可选包实例
     * @param additionalOfferList 可选包规格
     * @param mustProdSubOfferList 必选可选包
     * @param defaultProdSubOfferList 默选可选包
     * @return
     */
    protected void genProdSubOfferInstList(List<CuccProdSubOfferVO> additionalOfferInstList, List<Offer> additionalOfferList,
                                           List<ProdSubOfferVO> mustProdSubOfferList, List<ProdSubOfferVO> defaultProdSubOfferList) {
        for (CuccProdSubOfferVO offerInst : additionalOfferInstList) {
            if (AdditionalOfferUtils.getInst().isContainSubOffer(additionalOfferList, offerInst.getOfferId())) {
                mustProdSubOfferList.add(offerInst);
            }
            else {
                defaultProdSubOfferList.add(offerInst);
            }
        }
    }


    /**
     * 根据接入产品产品订购的可选包
     * @param sceneInstId 场景实例标识
     * @param prodInstId 接入产品实例标识
     * @param custId 客户标识
     * @return
     */
    protected List<OfferInstDTO> listAdditionalOfferInstsByProdInstId(String sceneInstId, String prodInstId, String custId) {
        //查询可选包实例
        IQuerySceneAdditionalOfferInstService querySceneAdditionalOfferInstService = SpringUtil.getBean(IQuerySceneAdditionalOfferInstService.class);
        //换成过滤掉状态为D的
        return querySceneAdditionalOfferInstService.listAdditionalOfferInstsInUseByProdInstId(sceneInstId, prodInstId, custId).getResultObject();
    }



    /**
     * 排序顺序
     *
     * @return
     */
    protected Map<String, Integer> getSortMap() {
        //必选-默选-同转-退订-加选
        Map<String, Integer> sortMap = new HashMap<>();
        sortMap.put(CoreDataOffer.OFF_C_0005_1200, 1);
        sortMap.put(CoreDataOffer.OFF_C_0005_1300, 2);
        sortMap.put(TemplateConsts.ADDITIONAL_OFFER_SAME, 3);
        sortMap.put(TemplateConsts.ADDITIONAL_OFFER_UNSUBSCRIBE, 4);
        sortMap.put(TemplateConsts.ADDITIONAL_OFFER_OPTION, 5);
        return sortMap;
    }


}
